package com.qitmiaojie.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qitmiaojie.controller.utils.Result;
import com.qitmiaojie.converters.JacksonObjectMapper;
import com.qitmiaojie.pojo.Lives;
import com.qitmiaojie.pojo.Weather;
import com.qitmiaojie.service.LeaveService;
import io.netty.handler.codec.json.JsonObjectDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class TestController {

    @Autowired
    private LeaveService leaveService;


    @GetMapping("/weather")
    public Result getWeather(HttpServletRequest request, HttpServletResponse response) throws IOException {
        URL url = new URL("https://restapi.amap.com/v3/weather/weatherInfo?city=370114&key=12fab44679bd3e3f95453d1c350b146b");
        HttpURLConnection urlCon= (HttpURLConnection)url.openConnection();
        String res="";
        Weather weather = null;
        StringBuffer buffer = new StringBuffer();
        if (200 == urlCon.getResponseCode()){
            log.info(urlCon.getResponseMessage());
            InputStream is = urlCon.getInputStream();
            InputStreamReader isr = new InputStreamReader(is,"utf-8");
            BufferedReader br = new BufferedReader(isr);
            String str = null;
            while ((str = br.readLine()) != null){
                buffer.append(str);
            }
            log.info(buffer.toString());
            weather = new ObjectMapper().readValue(buffer.toString(), Weather.class);
            System.out.println(weather);
            /**
             * {"status":"1","count":"1","info":"OK","infocode":"10000",
             * "lives":[{"province":"山东","city":"章丘区","adcode":"370114","weather":"晴","temperature":"32",
             * "winddirection":"南","windpower":"6","humidity":"54","reporttime":"2022-06-25 10:31:08"}]}
             */
          /* Weather weather = new Weather();
           weather.setCount("1");
           weather.setInfo("ok");
           weather.setStatus("1");
           weather.setInfocode("10000");
            Map<String,Object> map = new HashMap<>();
            Lives lives = new Lives();
            lives.setProvince("山东");
            lives.setCity("章丘");
            lives.setAdcode("370114");
            List<Lives> list = new ArrayList<>();
            list.add(lives);
            weather.setLives(list);
            String string = new ObjectMapper().writeValueAsString(weather);
            System.out.println(string);*/
        }
        return Result.success(weather);
    }

}
